package com.smh.ui;

import com.smh.Util.CodeUtil;
import com.smh.pojo.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.*;
import java.util.ArrayList;

public class LoginJFrame extends JFrame implements MouseListener {

    //创建一个集合存储正确的用户名和密码
    ArrayList<User> list = new ArrayList<>();
    //登录按钮
    JButton login = new JButton();
    //注册按钮
    JButton register = new JButton();
    //密码输入框
    JPasswordField password = new JPasswordField();
    //显示与隐藏密码
    JButton passwordJLabel = new JButton(new ImageIcon("pintugame\\image\\login\\显示密码.png"));
    //正确的验证码
    JLabel rightCode = new JLabel();
    //添加用户名输入框
    JTextField username = new JTextField();
    //验证码的输入框
    JTextField code = new JTextField();
    File file = new File("pintugame\\file\\user\\userinfo.txt");




    static {
        //如果文件中的数据不发生变化,当然是在静态代码块好了,
        //如果发生变化,还是放在构造方法里面吧
    }


    public LoginJFrame() throws HeadlessException, IOException {
        //加载容器
        initJFrame();
        //加载页面组件
        initLogin();
        //加载数据
        initData();


        this.setVisible(true);
    }

    private void initData() throws IOException {

        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        while ((line = br.readLine()) != null) {
            String[] split = line.split("&|=");
            list.add(new User(split[1], split[3], Integer.parseInt(split[5])));
        }
        br.close();
    }

    private void initLogin() {
        //1. 添加用户名文字
        JLabel usernameText = new JLabel(new ImageIcon("pintugame\\image\\login\\用户名.png"));
        usernameText.setBounds(116, 135, 47, 17);
        this.getContentPane().add(usernameText);

        //2.添加用户名输入框
        username.setBounds(195, 134, 200, 30);
        this.getContentPane().add(username);

        //3.添加密码文字
        JLabel passwordText = new JLabel(new ImageIcon("pintugame\\image\\login\\密码.png"));
        passwordText.setBounds(130, 195, 32, 16);
        this.getContentPane().add(passwordText);

        //4.密码输入框
        password.setBounds(195, 195, 200, 30);
        password.setEchoChar('*');
        this.getContentPane().add(password);

        //显示与隐藏密码
        passwordJLabel.setBounds(395, 195, 20, 30);
        //去除按钮的默认边框
        passwordJLabel.setBorderPainted(false);
        //去除按钮的默认背景
        passwordJLabel.setContentAreaFilled(false);

        //绑定事件
        passwordJLabel.addMouseListener(this);
        this.getContentPane().add(passwordJLabel);

        //验证码提示
        JLabel codeText = new JLabel(new ImageIcon("pintugame\\image\\login\\验证码.png"));
        codeText.setBounds(133, 256, 50, 30);
        this.getContentPane().add(codeText);

        //验证码的输入框
        code.setBounds(195, 256, 100, 30);
        this.getContentPane().add(code);

        //工具类
        String codeStr = CodeUtil.getCode();
        //设置内容
        rightCode.setText(codeStr);
        //位置和宽高
        rightCode.setBounds(300, 256, 50, 30);
        //绑定事件
        rightCode.addMouseListener(this);
        //添加到界面
        this.getContentPane().add(rightCode);

        //5.添加登录按钮
        login.setBounds(123, 310, 128, 47);
        login.setIcon(new ImageIcon("pintugame\\image\\login\\登录按钮.png"));
        //去除按钮的默认边框
        login.setBorderPainted(false);
        //去除按钮的默认背景
        login.setContentAreaFilled(false);
        //绑定事件
        login.addMouseListener(this);

        this.getContentPane().add(login);

        //6.添加注册按钮

        register.setBounds(256, 310, 128, 47);
        register.setIcon(new ImageIcon("pintugame\\image\\login\\注册按钮.png"));
        //去除按钮的默认边框
        register.setBorderPainted(false);
        //去除按钮的默认背景
        register.setContentAreaFilled(false);
        //绑定事件
        register.addMouseListener(this);

        this.getContentPane().add(register);

        //背景图片
        JLabel backgroudJLabel = new JLabel(new ImageIcon("pintugame\\image\\login\\background.png"));
        backgroudJLabel.setBounds(0, 0, 470, 390);
        this.getContentPane().add(backgroudJLabel);
        this.getContentPane().repaint();

    }

    //检测是否登录成功
    private boolean checkNameAndPasswordAndCode(String name, String password, String code, String codestr) throws IOException {
        if (name == null || name.isEmpty()) {
            showJDialog("请输入用户名");
            return false;
        }
        if (password == null || password.isEmpty()) {
            showJDialog("请输入密码");
            return false;
        }
        if (code == null || code.isEmpty()) {
            showJDialog("请输入验证码");
            return false;
        }

        if (!codestr.equalsIgnoreCase(code)) {
            showJDialog("验证码错误");
            return false;
        }
        for (int i = 0; i < list.size(); i++) {
            User user = list.get(i);
            if (!user.getUsername().equals(name)) {
                continue;
            }
            if (user.getCount() == 3) {
                showJDialog("用户已被锁定");
                return false;
            }
            if (!user.getPassword().equals(password)) {
                showJDialog("密码错误");
                user.setCount(user.getCount() + 1);
                saveData();

                return false;
            }
            //次数置为0
            user.setCount(0);
            saveData();
            return true;
        }
        showJDialog("未注册");
        return false;
    }

    private void saveData() throws IOException {
        //写回数据
        BufferedWriter bw = new BufferedWriter(new FileWriter(file));
        for (User user1 : list) {
            bw.write(user1.toString());
            bw.newLine();
        }
        bw.close();
    }


    private void initJFrame() {
        //大小
        this.setSize(488, 430);
        //设置页面标题
        this.setTitle("拼图 登录");
        //设置界面置顶
        this.setAlwaysOnTop(true);
        //设置界面剧中
        this.setLocationRelativeTo(null);
        //设置游戏关闭方式
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    //要展示用户名或密码错误
    public void showJDialog(String content) {
        //创建一个弹框对象
        JDialog jDialog = new JDialog();
        //给弹框设置大小
        jDialog.setSize(200, 150);
        //让弹框置顶
        jDialog.setAlwaysOnTop(true);
        //让弹框居中
        jDialog.setLocationRelativeTo(null);
        //弹框不关闭永远无法操作下面的界面
        jDialog.setModal(true);

        //创建Jlabel对象管理文字并添加到弹框当中
        JLabel warning = new JLabel(content);
        warning.setBounds(0, 0, 200, 150);
        jDialog.getContentPane().add(warning);

        //让弹框展示出来
        jDialog.setVisible(true);
    }


    //鼠标事件
    @Override
    public void mouseClicked(MouseEvent e) {
        Object o = e.getSource();
        if (o == rightCode) {
            rightCode.setText(CodeUtil.getCode());
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
        Object o = e.getSource();
        if (o == passwordJLabel) {
            passwordJLabel.setIcon(new ImageIcon("pintugame\\image\\login\\显示密码按下.png"));
            password.setEchoChar((char) 0);
        }
        if (o == login) {
            login.setIcon(new ImageIcon("pintugame\\image\\login\\登录按下.png"));
        }
        if (o == register) {
            register.setIcon(new ImageIcon("pintugame\\image\\login\\注册按下.png"));
        }

    }

    @Override
    public void mouseReleased(MouseEvent e) {
        Object o = e.getSource();
        if (o == passwordJLabel) {
            password.setEchoChar('*');
            passwordJLabel.setIcon(new ImageIcon("pintugame\\image\\login\\显示密码.png"));
        }
        if (o == login) {
            try {
                if (checkNameAndPasswordAndCode(username.getText(), password.getText(), code.getText(), rightCode.getText())) {
                    new GameJFrame();
                    this.setVisible(false);

                }
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            login.setIcon(new ImageIcon("pintugame\\image\\login\\登录按钮.png"));
            rightCode.setText(CodeUtil.getCode());
        }
        if (o == register) {
            try {
                new RegisterJFrame(list);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            this.setVisible(false);
            register.setIcon(new ImageIcon("pintugame\\image\\login\\注册按钮.png"));
        }

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}
